let departmentlist = (function () {
    let $tableBox = $('.tableBox'),
        $tbody = $tableBox.find('tbody')
    
    // 获取部门列表
    async function bindDepartment() {
        let result = await queryDepart()
        if (result.code != 0) return
        let str=``
        result.data.forEach((item,index) => {
            let {id,name,desc} = item
            str+=`<tr>
                <td class="w10">${index+1}</td>
                <td class="w20">${name}</td>
                <td class="w40">${desc}</td>
                <td class="w20" departId=${id}>
                    <a href="javascript:;" class="btnEdit">编辑</a>
                    <a href="javascript:;" class="btnDelete">删除</a>
                </td>
            </tr>`
        })
        $tbody.html(str)
    }
    function delegateHandle() {
        $tbody.click(async ev => {
            let target = ev.target
            let $target = $(target)
            let departId= $target.parent().attr('departId')
            if (target.className === 'btnEdit') {
                window.location.href='departmentadd.html?departId='+departId
                return
            }
            if (target.className === 'btnDelete') {
                let flag = confirm('确认要删除吗？')
                if(!flag) return
                let result = await axios.get('/department/delete', {
                    params: {
                        departmentId:departId
                    }
                })
                if (result.code == 0) {
                    localStorage.removeItem('department')
                    $target.parent().parent().remove()
                    alert('删除成功') 
                }
                return
            }
        })
    }

    return {
        init() {
            bindDepartment()
            delegateHandle()
        }
    }
})()
 departmentlist.init()
